//
//  MessageViewController.swift
//  WangPu
//
//  Created by xiaozhi on 15/11/1.
//  Copyright © 2015年 zhixiao. All rights reserved.
//

import UIKit

class MessageViewController: UIViewController {
    var listTable:UITableView!
    var dataArray:[NSDictionary] = []
    var noteInfo:UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        if let array = GlobalDataHelper.instance.messageArray{
            dataArray = array
        }
        self.title = "消息"
        let backItem = UIBarButtonItem(image: UIImage(named: "back_icon"), style:UIBarButtonItemStyle.Plain, target:self, action:"goBack")
        backItem.tintColor = UIColor.whiteColor()
        self.navigationItem.leftBarButtonItem = backItem
        self.automaticallyAdjustsScrollViewInsets = false
        self.view.backgroundColor = UIColor.whiteColor()
        
        listTable = UITableView(frame:CGRectMake(0, 64, view.bounds.size.width, view.bounds.size.height - 64), style: UITableViewStyle.Plain)
        listTable.delegate = self
        listTable.dataSource = self
        listTable.separatorStyle = UITableViewCellSeparatorStyle.None
        listTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "commentID")
        view.addSubview(listTable)
        if dataArray.count == 0{
            noteInfo = UILabel(frame: CGRectMake(0, (view.bounds.size.height-100)/2.0, view.bounds.size.width, 80))
            noteInfo.text = "您暂时还没有消息"
            noteInfo.font = UIFont.systemFontOfSize(15)
            noteInfo.textAlignment = NSTextAlignment.Center
            noteInfo.textColor = UIColor.darkGrayColor()
            view.addSubview(noteInfo)
        }

        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(animated: Bool){
        super.viewWillAppear(animated)
        navigationController?.navigationBarHidden = false 
    }
    func  goBack(){
        self.navigationController?.popViewControllerAnimated(true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
extension MessageViewController:UITableViewDataSource,UITableViewDelegate{
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return dataArray.count
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return 1
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
        return 20
    }
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?{
        let headView = UIView(frame: CGRectMake(0, 0, tableView.bounds.size.width, 20))
        let lbl = UILabel(frame: CGRectMake(10, 0, headView.bounds.size.width - 10, 20))
        headView.backgroundColor = commonBackgoundColor
        let dic = dataArray[section]
        lbl.text = dic["title"] as? String
        lbl.font = UIFont.systemFontOfSize(12)
        headView.addSubview(lbl)
        return headView
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("commentID", forIndexPath: indexPath)
        cell.textLabel?.frame = cell.contentView.bounds
        cell.textLabel?.textAlignment = NSTextAlignment.Left
        let dic = dataArray[indexPath.section]
        cell.textLabel?.text = dic["content"] as? String
        cell.textLabel?.font = UIFont.systemFontOfSize(13)
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        cell.accessoryType = UITableViewCellAccessoryType.None
        return cell;
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
        return 40
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){

    }
}